package com.gaogzhen.security.lock;

import lombok.extern.slf4j.Slf4j;
import org.openjdk.jol.info.ClassLayout;

import java.util.Vector;
import java.util.concurrent.locks.LockSupport;

@Slf4j(topic = "s.BatchRevoke")
public class BatchRevoke {
    static Thread t1, t2, t3;
    public static void main(String[] args) throws InterruptedException {
        Vector<Person> list = new Vector<>();
        int num = 39;
        t1 = new Thread(() -> {
            for (int i = 0; i < num; i++) {
                Person p = new Person();
                list.add(p);
                synchronized (p) {
                    log.debug(i + "\t" + ClassLayout.parseInstance(p).toPrintable());
                }
            }
            LockSupport.unpark(t2);
        }, "t1");
        t1.start();
        t2 = new Thread(() -> {
            LockSupport.park();
            for (int i = 0; i < num; i++) {
                Person p  = list.get(i);
                log.debug("==================");
                log.debug(i + "\t" + ClassLayout.parseInstance(p).toPrintable());
                synchronized (p) {
                    log.debug(i + "\t" + ClassLayout.parseInstance(p).toPrintable());
                }
                log.debug(i + "\t" + ClassLayout.parseInstance(p).toPrintable());
            }
            LockSupport.unpark(t3);

        }, "t2");
        t2.start();

        t3 = new Thread(() -> {
            LockSupport.park();
            log.debug("==================>");
            for (int i = 0; i < num; i++) {
                Person p  = list.get(i);
                log.debug("==================");
                log.debug(i + "\t" + ClassLayout.parseInstance(p).toPrintable());
                synchronized (p) {
                    log.debug(i + "\t" + ClassLayout.parseInstance(p).toPrintable());
                }
                log.debug(i + "\t" + ClassLayout.parseInstance(p).toPrintable());
            }

        }, "t3");
        t3.start();
        t3.join();
        log.debug(ClassLayout.parseInstance(new Person()).toPrintable());
    }
}
